/*
 * Created: Author: Aléx Carvalho / Date: Mar 6, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 00:38:01 -0300 (Fri, 12 Jun 2015) $
 */
package com.cpm.view.zk.tree.structure;

import com.cpm.ServiceLocator;
import com.cpm.aggregate.SessionAttribute;
import com.cpm.business.SemaphoreColor;
import com.cpm.context.SessionContext;
import com.cpm.view.IconKit;
import com.cpm.entities.Folder;
import com.cpm.entities.Indicator;
import com.cpm.entities.Node;
import com.cpm.entities.StrategicPlan;
import com.cpm.entities.id.IntegerId;
import com.cpm.view.CSSClass;
import com.cpm.view.zk.tree.AbstractTree;
import com.cpm.view.zk.tree.DefaultTreeNode;
import org.zkoss.zk.ui.event.DropEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

public class StructureTree extends AbstractTree<Node> {

    private StrategicPlan strategicPlan;
    private final StructureLoader structureLoader;

    private final int height = 30;

    public StructureTree() {
        structureLoader = ServiceLocator.lookup(StructureLoader.class);
    }

    public void setStrategicPlan(StrategicPlan strategicPlan) {
        this.strategicPlan = strategicPlan;
    }

    @Override
    protected void buildRoot(DefaultTreeNode root) {
        if (strategicPlan != null) {
            structureLoader.loadStructure(root, strategicPlan);
        }
    }

    @Override
    protected void render(Treeitem item, DefaultTreeNode<Node> treeNode, int i) {
        item.setValue(treeNode);

        Object object = treeNode.getData();

        Treerow row = item.getTreerow();

        if (row == null) {
            row = new Treerow();
            row.setParent(item);
        }

        row.setHeight(height + "px");
        row.setDroppable("true");
        row.setDraggable("true");

        Treecell cell = new Treecell();
        cell.setParent(row);
        cell.setStyle("white-space: nowrap; text-overflow: ellipsis;");
        cell.setTooltiptext(object.toString());
        cell.setSclass(CSSClass.NO_SELECT_TEXT.toString());

        row.setWidgetListener("onBind", "controlDrag(this, '" + arrowId + "');");

        row.addEventListener(Events.ON_DROP, (Event event) -> {

            Treeitem draggedItem = (Treeitem) ((DropEvent) event).getDragged().getParent();
            DefaultTreeNode<Node> draggedTreeNode = (DefaultTreeNode) draggedItem.getValue();

            int posY = ((DropEvent) event).getY();

            StructureTreeControlDrag.executeDrag(posY, height, draggedTreeNode, treeNode);
        });

        if (object instanceof Indicator) {
            Indicator ind = (Indicator) object;
            cell.setLabel(ind.getName());
            
            CSSClass semaphore = ServiceLocator.lookup(SemaphoreColor.class).getSemaphoreColorToIndicator(ind);
            cell.setIconSclass(IconKit.ATTENTION.getValue() + " " + CSSClass.STRUCTURE_INDICATOR + " " + semaphore );
        }
        else if (object instanceof Folder) {
            Folder folder = (Folder) object;
            cell.setLabel(folder.getName());
            
            CSSClass semaphore = ServiceLocator.lookup(SemaphoreColor.class).getSemaphoreColorToFolder(folder);
            cell.setIconSclass(IconKit.FOLDER.getValue() + " " + CSSClass.STRUCTURE_FOLDER + " " + semaphore);
        }
        else {
            cell.setLabel(object.toString());
        }
    }
}
